/*
 * LoginFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;


import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;

import com.insigma.yanwenlong.bookkeeping.pojo.Login;
import com.insigma.yanwenlong.bookkeeping.service.LoginService;
import com.insigma.yanwenlong.bookkeeping.service.LoginServiceImpl;
import com.insigma.yanwenlong.bookkeeping.util.EncrptUtils;

/**
 *
 * @author  __USER__
 */
public class LoginFrame extends javax.swing.JFrame {
	private LoginService loginService = new LoginServiceImpl();
	/** Creates new form LoginFrame */
	public LoginFrame() {
		initComponents();
		//居中显示
		setLocationRelativeTo(null);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		lblLoginPassword = new javax.swing.JLabel();
		jpfPassword = new javax.swing.JPasswordField();
		btnLogin = new javax.swing.JButton();
		jLabel2 = new javax.swing.JLabel();
		txtName = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel1
				.setText("\u6b22\u8fce\u4f7f\u7528\u7f51\u65b0\u5bb6\u5ead\u7406\u8d22\u7cfb\u7edf\u3010\u5355\u673a\u7248\u3011");

		lblLoginPassword
				.setText("\u8bf7\u8f93\u5165\u767b\u5f55\u5bc6\u7801\uff1a");

		btnLogin.setText("\u767b\u9646");
		btnLogin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnLoginActionPerformed(evt);
			}
		});

		jLabel2.setText("\u8bf7\u8f93\u5165\u7528\u6237\u540d\uff1a");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(jLabel1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 400,
								Short.MAX_VALUE)
						.addGroup(
								layout.createSequentialGroup().addGap(170, 170,
										170).addComponent(btnLogin)
										.addContainerGap(173, Short.MAX_VALUE))
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(19, 19, 19)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																false)
														.addComponent(
																jLabel2,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																lblLoginPassword,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(txtName)
														.addComponent(
																jpfPassword,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																174,
																Short.MAX_VALUE))
										.addContainerGap(107, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												jLabel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												25,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(39, 39, 39)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																txtName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(37, 37, 37)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																lblLoginPassword)
														.addComponent(
																jpfPassword,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(31, 31, 31).addComponent(
												btnLogin).addContainerGap(59,
												Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击登录，进入系统
	private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
		String frameName = txtName.getText();
		String framePassword =EncrptUtils.encryptToMD5(new String(jpfPassword.getPassword()));
		if("".equals(frameName) || "".equals(framePassword)) {
			JOptionPane.showMessageDialog(this, "用户名或密码不能为空");
			return ;
		}
		try {
			List<Login> logins = loginService.findLoginAccounts();
			//标记登录人员的个数
			int len = logins.size();
			//标记是否匹配
			int i = 0;
			for (Login login : logins) {
				if(login.getName().equals(frameName) && login.getPassword().equals(framePassword)) {
					this.setVisible(false);
					new BookkeepingMainFrame().setVisible(true);
					break;
				}
				i++;
				if(i == len) {
					JOptionPane.showMessageDialog(this, "用户名或密码错误");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new LoginFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnLogin;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPasswordField jpfPassword;
	private javax.swing.JLabel lblLoginPassword;
	private javax.swing.JTextField txtName;
	// End of variables declaration//GEN-END:variables

}